﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace Admin.WorkFlow.FormTable.TaskBook
{
    public partial class AddSubDepartMent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["OrganizeId"] != null && Request.Params["OrganizeId"].ToString() != "")
            {
                int organizeId = ChinaSoft.Common.Input.SetInt(Request.Params["OrganizeId"]);
                BindDepartMent(organizeId);
            }
            if (Request.Params["ProjectId"] != null && Request.Params["ProjectId"].ToString() != "")
            {
                int projectId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
                BindContractMan();
            }
        }
        /// <summary>
        /// 绑定部门
        /// </summary>
        /// <param name="organizeId"></param>
        private void BindDepartMent(int organizeId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent bllDepartMent = new ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent();
            DataSet ds = bllDepartMent.GetList("OrganizeId=" + organizeId);
            ChinaSoft.Common.BindControl.BindDropDownList(ddlXieBan, ds, "DepartMentName", "DepartMentId");
        }
        /// <summary>
        /// 绑定负责人
        /// </summary>
        private void BindContractMan()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bllUsers = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            DataSet ds = bllUsers.GetAllList();
            ChinaSoft.Common.BindControl.BindDropDownList(ddlContractMan, ds, "UserName", "UsersId");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //更新协办部门信息
            ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent modelXieBan = new ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bllXieBan = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            modelXieBan = bllXieBan.GetModel(ChinaSoft.Common.Input.SetInt(HidSubDepartMent.Value));
            modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlXieBan.SelectedItem.Value);
            modelXieBan.OutPut = ChinaSoft.Common.Input.SetDecimal(txtProductMoney2.Text.Trim());
            bllXieBan.Update(modelXieBan);
            //更新分项负责人信息
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.UpdateList("XieBanId=" + ChinaSoft.Common.Input.SetInt(ddlXieBan.SelectedItem.Value), "tb_MidSoft_SubContractUser", "XieBanId=" + ChinaSoft.Common.Input.SetInt(HidSubDepartMent.Value) + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
        }

        protected void ddlXieBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent modelXieBan = new ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bllXieBan = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            if (HidSubDepartMent.Value != "")
            {
                //修改
                modelXieBan = bllXieBan.GetModelList("TaskBookId=1 and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]))[0];
                modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt( ddlXieBan.SelectedItem.Value);
                modelXieBan.OutPut = ChinaSoft.Common.Input.SetDecimal(txtProductMoney2.Text.Trim());
                bllXieBan.Update(modelXieBan);
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                bllBind.UpdateList("XieBanId=" + ChinaSoft.Common.Input.SetInt(ddlXieBan.SelectedItem.Value), "tb_MidSoft_SubContractUser", "XieBanId=" + ChinaSoft.Common.Input.SetInt(HidSubDepartMent.Value) + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
               
                HidSubDepartMent.Value = ddlXieBan.SelectedItem.Value;
            }
            else
            {
                //添加
                modelXieBan.CreateTime = DateTime.Now;
                modelXieBan.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlXieBan.SelectedItem.Value);
                modelXieBan.DepartMentType = 1;
                modelXieBan.OutPut = ChinaSoft.Common.Input.SetDecimal(txtProductMoney2.Text.Trim());
                modelXieBan.TaskBookId = 0;
                bllXieBan.Add(modelXieBan);
            }
        }
        /// <summary>
        /// 添加项目负责人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnContractManAdd_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
            modelMainUser.CreateTime = DateTime.Now;
            modelMainUser.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            modelMainUser.SubContractUserId = ChinaSoft.Common.Input.SetInt(ddlXieBan.SelectedItem.Value);
            modelMainUser.XieBanId = ChinaSoft.Common.Input.SetInt(HidSubDepartMent.Value);
            bllMainUser.Add(modelMainUser);
        }

        protected void btnContractManDel_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
            int recordCount = bllMainUser.GetRecordCount("XieBanId=" + HidSubDepartMent.Value + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
            if (recordCount > 0)
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                bllBind.DeleteWhere("tb_MidSoft_SubContractUser", "SubContractId=(select   top   1   SubContractId   from   tb_MidSoft_SubContractUser where XieBanId=0 order by SubContractId Desc)  ");
            }
        }
    }
}